﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 定时关机
{
    public partial class FormMain : Form
    {
        private DateTime runTime;
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            tlSysOpenTime.Text = "";

            for (int i = 0; i < 100; i++)
            {
                cmbHour.Items.Add(i);
            }

            for (int i = 0; i < 60; i++)
            {
                cmbMinute.Items.Add(i);
                cmbSecond.Items.Add(i);
            }
            cmbHour.SelectedIndex = 0;
            cmbMinute.SelectedIndex = 0;
            cmbSecond.SelectedIndex = 5;
            btnCancel.Enabled = false;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (radDjs.Checked)
            {
                int hour = int.Parse(cmbHour.Text);
                int minute = int.Parse(cmbMinute.Text);
                int second = int.Parse(cmbSecond.Text);
                runTime = DateTime.Now.AddHours(hour).AddMinutes(minute).AddSeconds(second);
            }
            else if (radTime.Checked)
            {
                if (dtTime.Value <= DateTime.Now)
                {
                    MessageBox.Show("执行时间不能小于当前时间！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                runTime = dtTime.Value;
            }
            btnStart.Enabled = false;
            btnCancel.Enabled = true;
            timer1.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
            labLeaveTime.Text = string.Empty;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") == runTime.ToString("yyyy-MM-dd hh:mm:ss"))
            {
                if (radShutdown.Checked)
                {
                    Shutdown(0);
                }
                else if (radReboot.Checked)
                {
                    Restart(0);
                }
                else if (radLogoff.Checked)
                {
                    Logoff(0);
                }
                Cancel();
            }
            else
            {               
                TimeSpan sp = runTime.Subtract(DateTime.Now);
                if (sp.Ticks >= 0)
                {
                    labLeaveTime.Text = "剩余时间：" + sp.Days + "天" + sp.Hours + "小时" + sp.Minutes + "分" + sp.Seconds + "秒";
                }
            }
        }

        private void Cancel()
        {
            btnStart.Enabled = true;
            btnCancel.Enabled = false;
            timer1.Enabled = false;
        }
        /// <summary>
        /// 执行CMD命令
        /// </summary>
        /// <param name="cmd">cmd命令</param>
        private string ExecuteCmd(string cmd)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口
            p.Start();//启动程序

            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine(cmd + "&exit");

            p.StandardInput.AutoFlush = true;

            //获取cmd窗口的输出信息
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();

            return output;
        }
        /// <summary>
        /// cmd命令关机
        /// </summary>
        /// <param name="second">N秒后关机</param>
        private void Shutdown(int second)
        {
            string command = string.Format("shutdown -s -t {0}", second);
            ExecuteCmd(command);
        }
        /// <summary>
        /// cmd命令重启
        /// </summary>
        /// <param name="second"></param>
        private void Restart(int second)
        {
            string command = string.Format("shutdown -r -t {0}", second);
            ExecuteCmd(command);
        }
        /// <summary>
        /// cmd注销用户
        /// </summary>
        /// <param name="second"></param>
        private void Logoff(int second)
        {
            string command = string.Format("shutdown -l -t {0}", second);
            ExecuteCmd(command);
        }
    }
}
